Home:ALL Converter>When does the `newlines` attribute get set in universal newlines mode?

When does the `newlines` attribute get set in universal newlines mode?

Ask Time:2015-04-23T21:52:09         Author:Arek' Fu

Json Formatter

I have a text file with Windows-style line terminators (\r\n) which I open in universal newlines mode (Python 2.7). I would expect the newlines attribute to be set after the first call to the readline() method, but apparently this is not the case:

>>> f=open('test_crlf', 'rU')
>>> f.newlines
>>> f.readline()
'foo\n'
>>> f.newlines
>>> f.readline()
'bar\n'
>>> f.newlines
'\r\n'

On the other hand, the newlines attribute gets set after the first call to readline() on a file with Unix-style line endings.

What is the general rule? When should I expect the newlines attribute to be set?

Author:Arek' Fu,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29825570/when-does-the-newlines-attribute-get-set-in-universal-newlines-mode
yy